package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.condition.kmsscene.KmsSceneCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.kmsscene.KmsSceneCreateForm;
import com.bwda.dsrs.filecenter.domain.form.kmsscene.KmsSceneQueryForm;
import com.bwda.dsrs.filecenter.domain.form.kmsscene.KmsSceneUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.kmsscene.KmsScenePo;
import com.bwda.dsrs.filecenter.domain.vo.kmsscene.KmsSceneVo;
import com.bwda.dsrs.filecenter.service.KmsSceneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/kmsscene")
@Api(value = "/b/kmsscene", tags = "场景管理")
public class KmsSceneController extends BaseController {

	@Autowired
	private KmsSceneService kmsSceneService;

	@ApiOperation(value = "查询",notes = "查询",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KmsSceneVo> query(@ApiParam(value = "场景ID", required = true)@RequestParam Integer sceneId) throws BusinessServiceException {
		KmsScenePo po = kmsSceneService.queryWithValid(sceneId);
		KmsSceneVo vo = CopyUtil.transfer(po, KmsSceneVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数量",notes = "查询数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid KmsSceneQueryForm form) throws BusinessServiceException {
		KmsSceneCondition condition = this.getConditionByQueryForm(form);
		int count = kmsSceneService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询列表",notes = "查询列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<KmsSceneVo>> queryList(@ModelAttribute@Valid KmsSceneQueryForm form) throws BusinessServiceException {
		KmsSceneCondition condition = this.getConditionByQueryForm(form);
		List<KmsScenePo> poList = kmsSceneService.queryList(condition);
		List<KmsSceneVo> voList = CopyUtil.transfer(poList, KmsSceneVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询列表(带分页)",notes = "查询列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<KmsSceneVo>> queryPageList(@ModelAttribute@Valid KmsSceneQueryForm form) throws BusinessServiceException {
		KmsSceneCondition condition = this.getConditionByQueryForm(form);
		List<KmsSceneVo> voList = new ArrayList<>();
		int count = kmsSceneService.queryCount(condition);
		if (count > 0) {
			List<KmsScenePo> poList = kmsSceneService.queryList(condition);
			voList = CopyUtil.transfer(poList, KmsSceneVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增",notes = "新增",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KmsSceneVo> add(@ModelAttribute@Valid KmsSceneCreateForm form) throws BusinessServiceException {
		KmsScenePo po = CopyUtil.transfer(form, KmsScenePo.class);

		/**
		 * 场景-code名称是否已存在
		 */
		int count = kmsSceneService.countByTaskCode(po);
		if (count > 0) {
			return getFailResult("此编码已经存在");
		}

//		po.setSceneId(RandomUtils.nextInt());
		kmsSceneService.insert(po);
		KmsSceneVo vo = CopyUtil.transfer(po, KmsSceneVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改",notes = "修改",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid KmsSceneUpdateForm form) throws BusinessServiceException {
		KmsScenePo po = CopyUtil.transfer(form, KmsScenePo.class);
		/**
		 * 场景-code名称是否已存在
		 */
		int count = kmsSceneService.countByTaskCode(po);
		if (count > 0) {
			return getFailResult("此编码已经存在");
		}
		kmsSceneService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除",notes = "删除",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "场景ID", required = true)@RequestParam Integer[] sceneId) throws BusinessServiceException {
		kmsSceneService.deleteByIds(sceneId);
		return getSuccessResult();
	}

	/**
	 * KmsSceneQueryForm转换为KmsSceneCondition
	 *
	 * @param form
	 * @return
	 */
	private KmsSceneCondition getConditionByQueryForm(KmsSceneQueryForm form) {
		KmsSceneCondition condition = CopyUtil.transfer(form, KmsSceneCondition.class);
		return condition;
	}

}